#include "a_global_vars.h"
#include	"config_parser.h"

//------------------------------------------------------------------------------
void acceptor_0_deliver(int instance_number, int VALUE) {
	if (!data[(instance_number % INTERNAL_BUFFER_SIZE)].deliver && data[(instance_number % INTERNAL_BUFFER_SIZE)].received) {
		data[(instance_number % INTERNAL_BUFFER_SIZE)].dec = VALUE;
		data[(instance_number % INTERNAL_BUFFER_SIZE)].deliver = true;
	}
}
void learner_deliver() {
	int i;
	int ld = lowestDelivered;

	for (i = ld + 1; i < a_interval_performance->highest_deliverable_inst_id + 1; i++) {
		if (data[(i % INTERNAL_BUFFER_SIZE)].received) {
			if ((PROCESS_ID != get_config_info()->total_num_acceptors - 1) && !data[(i % INTERNAL_BUFFER_SIZE)].deliver) {

				a_interval_performance->interval_latency += getMiliSeconds() - data[(i % INTERNAL_BUFFER_SIZE)].packet.p_msgs[0].mili_seconds;
				data[(i % INTERNAL_BUFFER_SIZE)].dec = data[(i % INTERNAL_BUFFER_SIZE)].packet.pack_id;
				data[(i % INTERNAL_BUFFER_SIZE)].deliver = true;
				data[(i % INTERNAL_BUFFER_SIZE)].received = false;
				data[(i % INTERNAL_BUFFER_SIZE)].deliver = false;

				paxos_instances[(i % INTERNAL_BUFFER_SIZE)].vval = data[(i % INTERNAL_BUFFER_SIZE)].packet.pack_id;

				a_interval_performance->interval_num_packets_delivered++;
				lowestDelivered = i;

				if (PROCESS_ID == 0) {
					paxos_instances[(i % INTERNAL_BUFFER_SIZE)].vval = data[(i % INTERNAL_BUFFER_SIZE)].packet.pack_id;
				}
				//--------------------------------------------
//				if (!COORDINATOR) {
//					paxos_instances[(i % INTERNAL_BUFFER_SIZE)].rnd = 0;
//					paxos_instances[(i % INTERNAL_BUFFER_SIZE)].vrnd = 0;
//					paxos_instances[(i % INTERNAL_BUFFER_SIZE)].vval = 0;
//					paxos_instances[(i % INTERNAL_BUFFER_SIZE)].crnd = -1;
//					paxos_instances[(i % INTERNAL_BUFFER_SIZE)].cval = -1;
//				} else {
//					paxos_instances[(i % INTERNAL_BUFFER_SIZE)].rnd = 0;
//					paxos_instances[(i % INTERNAL_BUFFER_SIZE)].vrnd = 0;
//					paxos_instances[(i % INTERNAL_BUFFER_SIZE)].vval = 0;
//					paxos_instances[(i % INTERNAL_BUFFER_SIZE)].crnd = 1;// before =0
//					paxos_instances[(i % INTERNAL_BUFFER_SIZE)].cval = 0;
//				}
			} else if (PROCESS_ID == get_config_info()->total_num_acceptors - 1) {
				a_interval_performance->interval_latency += getMiliSeconds() - data[(i % INTERNAL_BUFFER_SIZE)].packet.p_msgs[0].mili_seconds;
				data[(i % INTERNAL_BUFFER_SIZE)].dec = data[(i % INTERNAL_BUFFER_SIZE)].packet.pack_id;
				data[(i % INTERNAL_BUFFER_SIZE)].received = false;
				data[(i % INTERNAL_BUFFER_SIZE)].deliver = false;

				paxos_instances[(i % INTERNAL_BUFFER_SIZE)].vval = data[(i % INTERNAL_BUFFER_SIZE)].packet.pack_id;

				a_interval_performance->interval_num_packets_delivered++;
				//---------------------------------------------------
//				paxos_instances[(i % INTERNAL_BUFFER_SIZE)].rnd = 0;
//				paxos_instances[(i % INTERNAL_BUFFER_SIZE)].vrnd = 0;
//				paxos_instances[(i % INTERNAL_BUFFER_SIZE)].vval = 0;
//				paxos_instances[(i % INTERNAL_BUFFER_SIZE)].crnd = -1;
//				paxos_instances[(i % INTERNAL_BUFFER_SIZE)].cval = -1;
				lowestDelivered = i;
			}
		} else
			break;
	}
}
//------------------------------------------------------------------------------
